package com.example.szh.mvp.view.live;

import android.os.Bundle;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.example.szh.R;
import com.example.szh.adapter.LiveCommentAdapter;
import com.example.szh.adapter.LiveVpAdapter;
import com.example.szh.db.DBManager;
import com.example.szh.entity.BaseEntity;
import com.example.szh.entity.LiveCommentEntity;
import com.example.szh.entity.UserEntity;
import com.example.szh.http.HttpCode;
import com.example.szh.mvp.contract.LREContract;
import com.example.szh.mvp.di.DaggerLREConponent;
import com.example.szh.mvp.di.LREMoudes;
import com.example.szh.mvp.presenter.LREPresenter;
import com.example.szh.nextwork.view.BaseActivity;
import com.example.szh.utils.ToastUtils;
import com.flyco.tablayout.SlidingTabLayout;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.tb.emoji.Emoji;
import com.tb.emoji.EmojiUtil;
import com.tb.emoji.FaceFragment;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LiveValuesActivity extends BaseActivity<LREPresenter> implements LREContract.ILREView
        , FaceFragment.OnEmojiClickListener ,View.OnClickListener{


    private StandardGSYVideoPlayer live_gsy_player;
    private SlidingTabLayout live_values_tab;
    private ViewPager live_values_vp;
    private LiveVpAdapter vpAdapter;
    private LiveCommentAdapter commentAdapter;
    private long live_id;
    private String live_url;
    private RelativeLayout liveMsgRelative;
    private RelativeLayout liveValuesEdtBtn;
    private ImageView liveValuesFaceImg;
    private RelativeLayout liveBottomView;
    private EditText liveMsgEdt;
    private TextView liveSendTv;
    private ImageView liveEmojiImg;
    private LinearLayout liveEmojiLinear;
    private List<UserEntity> list;

    @Override
    public View addStatusView() {
        return f(R.id.live_values_root);
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof LiveCommentEntity){
            LiveCommentEntity liveCommentEntity = (LiveCommentEntity) entity;
            vpAdapter.getRootRv().setAdapter(commentAdapter);
            if (liveCommentEntity.getValues() == null)
                return;
            commentAdapter.setNewData(liveCommentEntity.getValues());
        }else {
            if (entity.message.equals("发表成功")){
                //当前评论发布成功
                liveMsgEdt.setText("");
                initData();
            }
        }


    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {

    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_live_values;
    }

    @Override
    public void initView() {
        live_id = getIntent().getExtras().getLong("live_id");
        live_url = getIntent().getExtras().getString("live_url");
        live_gsy_player = f(R.id.live_gsy_player);
        live_values_tab = f(R.id.live_values_tab);
        live_values_vp = f(R.id.live_values_vp);
        vpAdapter = new LiveVpAdapter();
        live_values_vp.setAdapter(vpAdapter);
        live_values_tab.setViewPager(live_values_vp);
        commentAdapter = new LiveCommentAdapter();
        live_gsy_player.setUp(live_url,true,"");
        live_gsy_player.startPlayLogic();
        initListener();
        getSupportFragmentManager().beginTransaction().add(R.id.live_values_emoji_linear,
                FaceFragment.Instance()).commit();
        liveMsgRelative = (RelativeLayout) findViewById(R.id.live_values_msg_relative);
        liveValuesEdtBtn = (RelativeLayout) findViewById(R.id.live_values_edt_btn);
        liveValuesFaceImg = (ImageView) findViewById(R.id.live_values_face_img);
        liveBottomView = (RelativeLayout) findViewById(R.id.live_values_bottom_view);
        liveMsgEdt = (EditText) findViewById(R.id.live_values_msg_edt);
        liveSendTv = (TextView) findViewById(R.id.live_values_send_tv);
        liveEmojiImg = (ImageView) findViewById(R.id.live_values_emoji_img);
        liveEmojiLinear = f(R.id.live_values_emoji_linear);
        liveValuesEdtBtn.setOnClickListener(this::onClick);
        liveValuesFaceImg.setOnClickListener(this::onClick);
        liveEmojiImg.setOnClickListener(this::onClick);
        liveSendTv.setOnClickListener(this::onClick);
        liveMsgRelative.setOnClickListener(this::onClick);
    }

    private void initListener(){
        live_gsy_player.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("live_url",live_url);
                startPage(bundle,FullVideoActivity.class);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        live_gsy_player.getCurrentPlayer().onVideoResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        live_gsy_player.getCurrentPlayer().onVideoPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        live_gsy_player.getCurrentPlayer().release();
    }

    @Override
    public void inject() {
        DaggerLREConponent.builder().lREMoudes(new LREMoudes(this)).build().injectLiveValuesActivity(this);
    }

    @Override
    public void initData() {
        Map<String,Object> map=new HashMap<>();
        map.put("code", HttpCode.SELLIVECOMMENTCODE);
        map.put("live_id", 1);
        p.requestAll(map);
    }

    @Override
    public void onEmojiDelete() {
        String text = liveMsgEdt.getText().toString();
        if (text.isEmpty()) {
            return;
        }
        if ("]".equals(text.substring(text.length() - 1, text.length()))) {
            int index = text.lastIndexOf("[");
            if (index == -1) {
                int action = KeyEvent.ACTION_DOWN;
                int code = KeyEvent.KEYCODE_DEL;
                KeyEvent event = new KeyEvent(action, code);
                liveMsgEdt.onKeyDown(KeyEvent.KEYCODE_DEL, event);
                try {
                    EmojiUtil.handlerEmojiText(liveMsgEdt, liveMsgEdt.getText().toString(), this);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return;
            }
            liveMsgEdt.getText().delete(index, text.length());
            try {
                EmojiUtil.handlerEmojiText(liveMsgEdt, liveMsgEdt.getText().toString(), this);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        int action = KeyEvent.ACTION_DOWN;
        int code = KeyEvent.KEYCODE_DEL;
        KeyEvent event = new KeyEvent(action, code);
        liveMsgEdt.onKeyDown(KeyEvent.KEYCODE_DEL, event);
        try {
            EmojiUtil.handlerEmojiText(liveMsgEdt, liveMsgEdt.getText().toString(), this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onEmojiClick(Emoji emoji) {
        if (emoji != null) {
            int index = liveMsgEdt.getSelectionStart();
            Editable editable = liveMsgEdt.getEditableText();
            if (index < 0) {
                editable.append(emoji.getContent());
            } else {
                editable.insert(index, emoji.getContent());
            }
            try {
                EmojiUtil.handlerEmojiText(liveMsgEdt, liveMsgEdt.getText().toString(), this);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.live_values_edt_btn:
                liveBottomView.setVisibility(View.VISIBLE);
                liveEmojiLinear.setVisibility(View.GONE);
                break;
            case R.id.live_values_face_img:
                ToastUtils.showMsg("aaa");
                liveBottomView.setVisibility(View.VISIBLE);
                liveEmojiLinear.setVisibility(View.VISIBLE);
                break;
            case R.id.live_values_emoji_img:
                ToastUtils.showMsg("bbb");
                if (liveEmojiLinear.getVisibility()==View.VISIBLE){
                    liveEmojiLinear.setVisibility(View.GONE);
                    liveEmojiImg.setImageResource(R.drawable.res_7icon_129);
                }else {
                    liveEmojiLinear.setVisibility(View.VISIBLE);
                    liveEmojiImg.setImageResource(R.drawable.res_7icon_131);
                }
                break;
            case R.id.live_values_send_tv:
                if (liveMsgEdt.getText()==null){
                    ToastUtils.showMsg("请出入评论内容");
                    return;
                }
                if (liveMsgEdt.getText().toString().isEmpty()){
                    ToastUtils.showMsg("请出入评论内容");
                    return;
                }
                try {
                    list = DBManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Map<String,Object> map=new HashMap<>();
                map.put("code", HttpCode.ADDLIVECOMMENTCODE);
                map.put("comment_time",System.currentTimeMillis()/1000);
                map.put("comment_value",liveMsgEdt.getText().toString());
                map.put("user_id",list.get(0).getUser_id());
                map.put("live_id", live_id);
                map.put("lc_id", 1);
                p.requestAll(map);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            if (liveBottomView.getVisibility()==View.VISIBLE){
                liveBottomView.setVisibility(View.GONE);
                return true;
            }else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}